package com.subway.babynote;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import com.subway.babynote.syncservice.syncapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class weightinput extends Activity implements OnClickListener {
	List<GridInfo> list;
	GridAdapter adapter;
	GridView gridview;
	EditText weight_et;
	TextView hint_view;
	Intent In;
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.weightinput);
		setTitle(R.string.weight);
		weight_et = (EditText) findViewById(R.id.weight_edit);
		weight_et.setFocusable(true);
		weight_et.setInputType(InputType.TYPE_CLASS_NUMBER);
		weight_et.setVisibility(View.GONE);
		List<Float> l = syncapi.getSyncImpl().getWeightSuggestion().l;
		Iterator<Float> it=l.iterator();
		gridview = (GridView) findViewById(R.id.gridview);
		list = new ArrayList<GridInfo>();
		while(it.hasNext()){
		  list.add(new GridInfo(it.next().toString()));
		
		}
		list.add(new GridInfo("other"));
		adapter = new GridAdapter(this);  
		adapter.setList(list);  
		gridview.setAdapter(adapter);  
		In = new Intent();
		weight_et.setOnKeyListener(new EditText.OnKeyListener() {
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				// TODO Auto-generated method stub
				Log.v("babynote", "keycode=" + keyCode);
				switch (keyCode) {
				case KeyEvent.KEYCODE_ENTER:
					Log.v("BabyNote", "weight confirm ...");
                    float weight=Float.parseFloat(weight_et.getText().toString());
					syncapi.getSyncImpl().AddWUser(weight);

					In.setClass(weightinput.this, weightinfo.class);
					
				//	startActivityForResult(In,RG_AMOUNT);
				
					startActivity(In);
					return true;
				}
				return false;
			}
		});
	}
	
	class ItemClickListener implements OnItemClickListener {
		public void onItemClick(AdapterView<?> arg0,// The AdapterView where the
													// click happened
				View arg1,// The view within the AdapterView that was clicked
				int arg2,// The position of the view in the adapter
				long arg3// The row id of the item that was clicked
		) {
			// 在本例中arg2=arg3
			HashMap<String, Object> item = (HashMap<String, Object>) arg0
					.getItemAtPosition(arg2);
			// 显示所选Item的ItemText
			String itemtext = (String) item.get("ItemText");
			if (itemtext.equals("other")) {
				weight_et.setVisibility(View.VISIBLE);

			} else {
				try {
					float weight = Float.parseFloat(itemtext);
					System.out.println("float height = " + weight);
					syncapi.getSyncImpl().AddWUser(weight);
					In.setClass(weightinput.this, weightinfo.class);
					startActivity(In);

				} catch (NumberFormatException nfe) {
					System.out.println("NumberFormatException: "
							+ nfe.getMessage());
				}
			}

		}
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
	}
	
	
}